package com.yuke.cloud.service.uac.security;

import com.yuke.cloud.common.security.auth.app.authentication.openid.UserInfoService;
import com.yuke.cloud.common.security.core.SecurityUser;
import com.yuke.cloud.service.uac.entity.AccUser;
import com.yuke.cloud.service.uac.service.AccUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;

/**
 * The class Uac user details service.
 *
 * @author
 */
@Component
@Slf4j
public class UacUserInfoServiceImpl implements UserInfoService {

	@Resource
	private AccUserService accUserService;

	@Override
	public UserDetails findUserByOpenId(String openId){
		AccUser user = accUserService.findByOpenId(openId);
		if (user == null) {
			throw new BadCredentialsException("用户不存在或者用户微信未绑定小程序！");
		}
		Collection<GrantedAuthority> grantedAuthorities;
		grantedAuthorities = accUserService.loadUserAuthorities(user.getUserId());
		return new SecurityUser(user.getUserId(), user.getLoginName(), user.getUserPassword(),
				user.getUserName(), user.getGroupId(), user.getGroupName(), user.getIsEnable(), grantedAuthorities);
	}
}
